local table_insert = table.insert
local math_random = math.random
local table_remove = table.remove
local zjh_rule = require "zhajinhua.zjh_rule"
local hhdz = require "zhajinhua.hhdz"
local sharedata = require "skynet.sharedata"
local skynet = require("skynet")
local M = {}
local GEN_CARD = {}

local function get_side_card_type(mix_index)
    local side = mix_index >> 16
    local card_type = mix_index & 15
    return side,card_type
end

function GEN_CARD:get_side_cards(mix_index)
	print("============",mix_index)
	local side,card_type = get_side_card_type(mix_index)
	print("get card side",mix_index,card_type)
	
	--另一幅牌不能比当前的牌型大
	local max_range = hhdz.CARD_TYPE_PROBABILITY[card_type][2]
	local other_random = math_random(1,max_range)

	local other_card_type = 0
	for card_type, random_list in pairs(hhdz.CARD_TYPE_PROBABILITY) do
		if other_random >= random_list[1] and other_random <= random_list[2] then
			other_card_type = card_type
			break
		end
	end

	-- return self:get_side_no_normal_card(card_type,other_card_type,side)

	local cards, other_cards = skynet.call(".cards_library", "lua", "get_side_no_normal_card", card_type, other_card_type, side)
	return cards, other_cards
end


--[[
function GEN_CARD:is_crads_equal(cards, other_cards)
	for _, card in pairs(cards) do
    	for _, other_card in pairs(other_cards) do
    		if card == other_card then
    			return true
    		end
    	end
    end
    return false
end

function GEN_CARD:get_side_no_normal_card(card_type,other_card_type,side)
	local internel_hhdz_cards = sharedata.query("internel_hhdz_cards")
	local card_list = internel_hhdz_cards[card_type]
	local card_index = math_random(1,#card_list)
	local cards = card_list[card_index]

	local other_card_list = internel_hhdz_cards[other_card_type]
	-- local index_list = {}
	-- for index, _ in pairs(other_card_list) do
	-- 	table_insert(index_list,index)
	-- end
	
	local find = false
	local other_index
	local i = 1
	repeat
		other_index = math_random(1,#other_card_list)
        -- other_index = index_list[index]
        local other_cards = other_card_list[other_index]
        local is_equal = self:is_crads_equal(cards, other_cards) --判断两幅牌是否存在相同点数的牌
        if not is_equal then
        	if not zjh_rule.is_draw(cards,other_cards) then --不能出现平局
        		find = true
        	end
        end
 
        if not find then
        	i = i + 1
        end
    until(i >= 50 or find)
    DEBUG("forge find get==========",i)
    --50次如果找不到强制搜索
    if i >= 50 and not find then
    	for index, other_cards in pairs(other_card_list) do
    		local is_equal = self:is_crads_equal(cards, other_cards)
    		if not is_equal then
    			if not zjh_rule.is_draw(cards,other_cards) then
    				other_index = index
    				break
    			end
    		end
    	end
   	end
	
	local red_card_type = zjh_rule.get_card_type(card_list[card_index])
	local black_card_type = zjh_rule.get_card_type(other_card_list[other_index])
	local win = zjh_rule.compare_card(red_card_type,card_list[card_index],black_card_type,other_card_list[other_index])
	if (win and side == 1) or (not win and side == 2) then
		return card_list[card_index],other_card_list[other_index]
	end
	
	if (not win and side == 1 ) or (win and side == 2) then
		return other_card_list[other_index],card_list[card_index]
	end
	
	--再做一次随机
    local side_random = math_random(1,100)
    if side_random >= 50 then
		return card_list[card_index],other_card_list[other_index]
	else
		return other_card_list[other_index],card_list[card_index]
	end
	
end

--]]

function M.new()
    local o = {}
    return setmetatable(o,{__index = GEN_CARD})
end

return M